from haystack import indexes
from shop.models import course


class courseIndex(indexes.SearchIndex, indexes.Indexable):
    text = indexes.CharField(document=True, use_template=False)  # 全文搜索
    id = indexes.IntegerField(model_attr='id')
    title = indexes.CharField(model_attr='title')
    realprice = indexes.FloatField(model_attr='realprice')
    price = indexes.FloatField(model_attr='price')
    students = indexes.IntegerField(model_attr='students')
    intro = indexes.CharField(model_attr='intro')
    detail = indexes.CharField(model_attr='detail')
    cover = indexes.CharField(model_attr='cover')

    # es索引名 = indexes.索引数据类型(model_attr="ORM中的字段名")
    def get_model(self):
        return course

    def index_queryset(self, using=None):
        return self.get_model().objects.all()  # 只索引未删除的课程

    # 设置text字段使用模板
    def prepare_text(self, obj):
        return obj.detail+obj.intro+obj.title
